﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Web;

public partial class Admin_UserManagement_AppointmentJ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            if (Session["users"] != null) 
            {
                DateTime time = DateTime.Now;
                DateTime times = DateTime.Parse("2011-7-30");
                if (time > times)  //验证是否时间过期
                {
                    this.btnEbit.Enabled = false;
                    this.btnEbit.Text = "时间已经过期";
                }
                getTime();//验证人数是否已满

                ViewState["userid"] = Session["users"].ToString();
                string sql = "select * from MedicalAppointment where userid='" + ViewState["userid"].ToString() + "' and BookingStatus != 1";
                DataTable tableSql = DBHelper.GetDataSet(sql);

                if (tableSql.Rows.Count > 0)  //验证是否预约
                {
                    if (tableSql.Rows.Count > 0)
                    {
                        this.btnEbit.Enabled = false;
                        this.btnEbit.Text = "您已预约";
                    }
                     if (tableSql.Rows[0]["BookingStatus"].ToString().Equals("2"))
                    {
                        this.btnEbit.Enabled = false;
                        this.btnEbit.Text = "您已体检";
                    }
                }
            }
            
        }
    }
    protected void btnEbit_Click(object sender, EventArgs e)
    {
        if (ViewState["userid"] != null)
        {

            string sql1 = "select * from MedicalAppointment where UserID ='" + ViewState["userid"] + "'";
            DataTable table1 = DBHelper.GetDataSet(sql1);
            if (table1.Rows.Count > 0) //有预约体检
            {
                string sql = "update MedicalAppointment set ReservationTime = '" + this.DropDownList1.SelectedValue + "',SetMeal='" + this.DropDownList2.SelectedValue
                    + "',AgencyName='" + Label1.Text + "',AgencyAddress='" + Label2.Text + "',ReservationNum='" + Label4.Text + "',BookingStatus=0 where userid = '" + ViewState["userid"] + "'";
                DBHelper.ExecuteCommand(sql);
            }
            else
            {
                string sql = "insert into MedicalAppointment values('" + ViewState["userid"].ToString() + "','" + this.DropDownList1.SelectedValue + "','" + this.DropDownList2.SelectedValue
                    + "','" + Label1.Text + "','" + Label2.Text + "','" + Label3.Text + "','" + Label4.Text + "','0')";
                Web.DBHelper.GetScalar(sql);
            }
            Web.CommFun.Show_Msg("预约成功！", "userManage.aspx");
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        getTime();
    }
    
    protected void btnEbit0_Click(object sender, EventArgs e)
    {
        Response.Redirect("AppointmentEdit.aspx?userId=" + Session["users"]);
    }

    private void getTime()
    {
        string time = this.DropDownList1.SelectedValue;  //获得时间
        string sql = "select COUNT(*)as coutns from MedicalAppointment where ReservationTime ='" + time + "' and AgencyName = '瑞慈体检-静安店' and BookingStatus!=1";
        DataTable tableTime = DBHelper.GetDataSet(sql);
        this.lblRen.Text = tableTime.Rows[0]["coutns"].ToString();
        if (tableTime.Rows.Count >= 50) //人数超过上线
        {
            this.btnEbit.Enabled = false;
            this.btnEbit.Text = "预约人数已满";
        }
    }
}
